package com.cloud.lan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.lan.api.dto.req.LanGoodsReq;
import com.cloud.lan.dao.LanGoodsImgMapper;
import com.cloud.lan.dao.LanGoodsLocationsMapper;
import com.cloud.lan.dao.LanGoodsMapper;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.model.dataobject.LanGoodsDO;
import com.cloud.lan.entity.model.dataobject.LanGoodsImgDO;
import com.cloud.lan.entity.model.dataobject.LanGoodsLocationsDO;
import com.cloud.lan.entity.model.pojo.LanGoodsLocationsResp;
import com.cloud.lan.service.ILanGoodsService;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ONE;
import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.TOW;

/**
 * <p>
 * 产品 服务实现类
 * </p>
 *
 * @author xyqierkang@163.com
 * @since 2021/05/10
 */
@Slf4j
@Service
public class LanGoodsServiceImpl extends ServiceImpl<LanGoodsMapper, LanGoodsDO> implements ILanGoodsService {

    @Resource
    LanGoodsMapper lanGoodsMapper;

    @Resource
    LanGoodsImgMapper lanGoodsImgMapper;

    @Resource
    LanGoodsLocationsMapper lanGoodsLocationsMapper;

    @Override
    public BaseResponse<Object> getGoodsList(LanGoodsReq req) {
        log.info("#查询 商品信息表 列表 {}", req);
        LambdaQueryWrapper<LanGoodsDO> qw = new LambdaQueryWrapper<>();
        qw.eq(LanGoodsDO::getIsDeleted, false).orderByDesc(LanGoodsDO::getCreateTime);
        Page<LanGoodsDO> page = new Page<>(req.getPageIndex(), req.getPageSize());
        IPage<LanGoodsDO> pageList = lanGoodsMapper.selectPage(page, qw);
        pageList.getRecords().forEach(g -> {
            List<LanGoodsImgDO> photos = lanGoodsImgMapper.selectList(new LambdaQueryWrapper<LanGoodsImgDO>()
                    .eq(LanGoodsImgDO::getIsDeleted, false).
                    eq(LanGoodsImgDO::getImgType, ONE.getKey()).
                    eq(LanGoodsImgDO::getGoodsId, g.getId()));
            List<LanGoodsImgDO> details = lanGoodsImgMapper.selectList(new LambdaQueryWrapper<LanGoodsImgDO>()
                    .eq(LanGoodsImgDO::getIsDeleted, false).
                    eq(LanGoodsImgDO::getImgType, TOW.getKey()).
                    eq(LanGoodsImgDO::getGoodsId, g.getId()));
            List<LanGoodsLocationsDO> locations = lanGoodsLocationsMapper.selectList(new LambdaQueryWrapper<LanGoodsLocationsDO>()
                    .eq(LanGoodsLocationsDO::getIsDeleted, false).
                    eq(LanGoodsLocationsDO::getGoodsId, g.getId()));
            g.setPhotos(photos.stream().map(LanGoodsImgDO::getImgPath).collect(Collectors.toList()));
            g.setDetails(details.stream().map(LanGoodsImgDO::getImgPath).collect(Collectors.toList()));

            List<LanGoodsLocationsResp> locationsListResp = new ArrayList<>();
            locations.forEach(lo -> {
                LanGoodsLocationsResp lanGoodsLocationsResp = new LanGoodsLocationsResp();
                lanGoodsLocationsResp.setLocationId(lo.getStoreId());
                lanGoodsLocationsResp.setLink(lo.getLocations());
                locationsListResp.add(lanGoodsLocationsResp);
            });
            g.setLocations(locationsListResp);
        });
        return BaseResponse.success(pageList);
    }
}
